package encounter;

import com.item.manager.Template;
import item.Item;
import item.ItemProvider;
import main.Hero;
import main.Logger;
import main.RandomProvider;

import java.util.Arrays;
import java.util.List;

public class NatureSeachEncounter implements Encounter, Lootable {
    private final Item item;
    private final int spotDifficulty;
    private final int natureDifficulty;

    public NatureSeachEncounter(ItemProvider provider, Template template) {
        this.item = provider.create(template.findAttr("ItemRef").asString());
        this.spotDifficulty = template.findAttr("spotDifficulty").asInt();
        this.natureDifficulty = template.findAttr("natureDifficulty").asInt();
    }

    @Override
    public boolean resolve(Hero hero) {
        boolean gathered = false;

        int spotRoll = RandomProvider.roll(hero.getSpot());
        int concealRoll = RandomProvider.roll(spotDifficulty);

        int natureRoll = RandomProvider.roll(hero.getNature());
        int knowledgeRoll = RandomProvider.roll(natureDifficulty);

        if (spotRoll > concealRoll && natureRoll > knowledgeRoll) {
            Logger.log(hero + " found " + item);
            hero.addItem(item, 1);
            gathered = true;
        }

        hero.updateEnergy(-0.01);
        return false;
    }

    @Override
    public Item getLoot() {
        return this.item;
    }

    @Override
    public List<Item> getPossibleLoot() {
        return Arrays.asList(item);
    }

}
